package com.zy.imagineCommunity.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 创建一个拦截器并实现HandlerInterceptor
 */
@Component
public class AdminLoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //Session中没有user对象就转到登陆
        Object adminUser = request.getSession().getAttribute("adminUser");

        if (adminUser == null) {
            System.out.println("没有权限请先admin登陆!");
            //未登陆，返回登陆界面
            request.getRequestDispatcher("/AdminLogin").forward(request, response);
            return false;
        } else {
            //已登陆，放行请求
            return true;
        }

    }

}
